package com.jyxd.web.controller.czform;

import com.alibaba.fastjson.JSON;
import com.jyxd.web.data.czform.CRRTAssess;
import com.jyxd.web.data.czform.CRRTNurse;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.service.czform.CRRTAssessService;
import com.jyxd.web.service.czform.CRRTNurseService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "crrt表单")
@Controller
@RequestMapping(value = "/crrtAccess")
public class CRRTAssessController {

    private static Logger logger = LoggerFactory.getLogger(CRRTAssessController.class);

    @Autowired
    private CRRTAssessService crrtAssessService;
    @Autowired
    private CRRTNurseService crrtNurseService;

    /**
     * 增加/修改一条CRRT前评估信息
     *
     * @return
     */
    @ApiOperation("增加一条CRRT前评估信息")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult insert (@RequestBody CRRTAssess crrtAssess) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        boolean flag = false;
        if (StringUtils.isEmpty(crrtAssess.getId())){
            crrtAssess.setId(UUIDUtil.getUUID());
             flag = crrtAssessService.insert(crrtAssess);
        }else{
            CRRTAssess crrtAssess1 = crrtAssessService.queryData(crrtAssess.getId());
            //swap(crrtAssess1,crrtAssess);
            flag = crrtAssessService.update(crrtAssess);
        }
        if (flag) {
            return new HttpResult(200,crrtAssess, "添加成功");
        } else {
            return new HttpResult(200,crrtAssess, "添加失败");
        }
    }


    /**
     * 编辑CRRT前评估信息
     *
     * @param map
     * @return
     */
    @ApiOperation("编辑CRRT前评估信息")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        if (map != null && map.containsKey("id") && map.containsKey("status")) {
            CRRTAssess crrtAssess = crrtAssessService.queryData(map.get("id").toString());
            if (crrtAssess != null) {
                crrtAssess.setStatus((int) map.get("status"));
                crrtAssessService.update(crrtAssess);
            } else {
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 删除CRRT前评估信息
     *
     * @param map
     * @return
     */
    @ApiOperation("删除CRRT前评估信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        if (map != null && map.containsKey("id")) {
            CRRTAssess crrtAssess = crrtAssessService.queryData(map.get("id").toString());
            if (crrtAssess != null) {
                crrtAssess.setStatus(- 1);
                crrtAssessService.update(crrtAssess);
                Map<String, Object> selectMap = new HashMap<String, Object>();
                selectMap.put("crrtAssessId",map.get("id").toString());
                List<CRRTNurse> list = crrtNurseService.getList(selectMap);
                if (list.size()>0){
                    for (int i = 0;i<list.size();i++){
                        list.get(i).setStatus("-1");
                        crrtNurseService.saveOrUpdateCRRTNurse(list.get(i));
                    }
                }
            } else {
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据主键id查询CRRT前评估信息
     *
     * @param map
     * @return
     */
    @ApiOperation("根据主键id查询CRRT前评估信息")
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    @ResponseBody
    public String queryData (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        if (map != null && map.containsKey("id")) {
            CRRTAssess crrtAssess = crrtAssessService.queryData(map.get("id").toString());
            //Map<String, Object> selectMap = new HashMap<String, Object>();
            //selectMap.put("crrtAssessId",map.get("id").toString());
            //List<CRRTNurse> list = crrtNurseService.getList(selectMap);
            if (crrtAssess != null) {
                //crrtAssess.setCrrtNurseList(list);
                json.put("data", JSON.toJSONString(crrtAssess));
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    @ApiOperation("获取最新一条CRRT前评估信息")
    @RequestMapping(value = "/queryLastData", method = RequestMethod.POST)
    @ResponseBody
    public String queryLastData (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        if (map != null && map.containsKey("patientId")) {
            json.put("code", HttpCode.OK_CODE.getCode());
            CRRTAssess crrtAssess = crrtAssessService.queryLastData(map);
            if (crrtAssess != null) {
                json.put("data", JSON.toJSONString(crrtAssess));
            }
        }
        return json.toString();
    }

    /**
     * 根据条件分页查询CRRT前评估信息列表（也可以不分页）
     *
     * @param map
     * @return
     */
    @ApiOperation("根据条件分页查询CRRT前评估信息列表（也可以不分页）")
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        if (map != null && map.containsKey("start")) {
            int totalCount = crrtAssessService.queryNum(map);
            map.put("start", ((int) map.get("start") - 1) * (int) map.get("size"));
            json.put("totalCount", totalCount);
        }
        List<CRRTAssess> list = crrtAssessService.queryList(map);
        if (list != null && list.size() > 0) {
            json.put("data", JSON.toJSONString(list));
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }


}
